home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch12 / vbdScrib.cls < prev    next >
Text File  |  1999-06-18  |  7KB  |  220 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "vbdScribble"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15. ' VbDraw Line/Rectangle object.
  16.  
  17. Implements vbdObject
  18.  
  19. ' Indicates a box rather than a line.
  20. Public IsBox As Boolean
  21.  
  22. ' The surface on which the user is clicking
  23. ' to define the object. This is set only during
  24. ' creation of this object.
  25. Public WithEvents m_Canvas As PictureBox
  26. Attribute m_Canvas.VB_VarHelpID = -1
  27. Private m_DrawingStarted As Boolean
  28.  
  29. ' Constituent vbdPolygon object.
  30. Private m_Polygon As vbdPolygon
  31. Private m_Object As vbdObject
  32.  
  33. ' Rubberband variables.
  34. Private m_StartX As Single
  35. Private m_StartY As Single
  36. Private m_LastX As Single
  37. Private m_LastY As Single
  38.  
  39. ' Start the scribble.
  40. Private Sub m_Canvas_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  41.     ' Start drawing the scribble.
  42.     m_DrawingStarted = True
  43.  
  44.     ' Create the vbdPolygon that represents us.
  45.     Set m_Polygon = New vbdPolygon
  46.     Set m_Object = m_Polygon
  47.     m_Polygon.IsClosed = False
  48.  
  49.     ' Save this point.
  50.     m_Polygon.NumPoints = 1
  51.     m_Polygon.X(m_Polygon.NumPoints) = X
  52.     m_Polygon.Y(m_Polygon.NumPoints) = Y
  53.  
  54.     ' Draw the line.
  55.     m_Canvas.CurrentX = X
  56.     m_Canvas.CurrentY = Y
  57.  
  58.     ' Remember where we are.
  59.     m_LastX = X
  60.     m_LastY = Y
  61. End Sub
  62.  
  63. ' Continue the scribble.
  64. Private Sub m_Canvas_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  65.     If Not m_DrawingStarted Then Exit Sub
  66.  
  67.     ' Make sure we really moved.
  68.     If m_LastX = X And m_LastY = Y Then Exit Sub
  69.  
  70.     ' Save this point.
  71.     m_Polygon.NumPoints = m_Polygon.NumPoints + 1
  72.     m_Polygon.X(m_Polygon.NumPoints) = X
  73.     m_Polygon.Y(m_Polygon.NumPoints) = Y
  74.  
  75.     ' Draw the line.
  76.     m_Canvas.Line -(X, Y)
  77.  
  78.     ' Remember where we are.
  79.     m_LastX = X
  80.     m_LastY = Y
  81. End Sub
  82. ' Finish the scribble.
  83. Private Sub m_Canvas_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  84.     If Not m_DrawingStarted Then Exit Sub
  85.     m_DrawingStarted = False
  86.  
  87.     ' Stop receiving events from the canvas.
  88.     Set m_Canvas = Nothing
  89.  
  90.     ' Tell the form to save us.
  91.     frmVbDraw.AddObject Me
  92. End Sub
  93. ' Add this transformation to the current one.
  94. Private Sub vbdObject_AddTransformation(M() As Single)
  95.     m_Object.AddTransformation M
  96. End Sub
  97.  
  98. Private Property Set vbdObject_Canvas(ByVal RHS As PictureBox)
  99.     Set m_Canvas = RHS
  100. End Property
  101.  
  102. Private Property Get vbdObject_Canvas() As PictureBox
  103.     Set vbdObject_Canvas = m_Canvas
  104. End Property
  105.  
  106. ' Clear the object's transformation.
  107. Private Sub vbdObject_ClearTransformation()
  108.     m_Object.ClearTransformation
  109. End Sub
  110.  
  111. ' Draw the object in a metafile.
  112. Private Sub vbdObject_DrawInMetafile(ByVal mf_dc As Long)
  113.     m_Object.DrawInMetafile mf_dc
  114. End Sub
  115. ' Return the object's DrawWidth.
  116. Public Property Get vbdObject_DrawWidth() As Integer
  117.     vbdObject_DrawWidth = m_Object.DrawWidth
  118. End Property
  119. ' Set the object's DrawWidth.
  120. Public Property Let vbdObject_DrawWidth(ByVal new_value As Integer)
  121.     m_Object.DrawWidth = new_value
  122. End Property
  123.  
  124. ' Return the object's DrawStyle.
  125. Public Property Get vbdObject_DrawStyle() As DrawStyleConstants
  126.     vbdObject_DrawStyle = m_Object.DrawStyle
  127. End Property
  128. ' Set the object's DrawStyle.
  129. Public Property Let vbdObject_DrawStyle(ByVal new_value As DrawStyleConstants)
  130.     m_Object.DrawStyle = new_value
  131. End Property
  132.  
  133. ' Return the object's ForeColor.
  134. Public Property Get vbdObject_ForeColor() As OLE_COLOR
  135.     vbdObject_ForeColor = m_Object.ForeColor
  136. End Property
  137. ' Set the object's ForeColor.
  138. Public Property Let vbdObject_ForeColor(ByVal new_value As OLE_COLOR)
  139.     m_Object.ForeColor = new_value
  140. End Property
  141.  
  142. ' Return the object's FillColor.
  143. Public Property Get vbdObject_FillColor() As OLE_COLOR
  144.     vbdObject_FillColor = m_Object.FillColor
  145. End Property
  146. ' Set the object's FillColor.
  147. Public Property Let vbdObject_FillColor(ByVal new_value As OLE_COLOR)
  148.     m_Object.FillColor = new_value
  149. End Property
  150.  
  151. ' Return the object's FillStyle.
  152. Public Property Get vbdObject_FillStyle() As FillStyleConstants
  153.     vbdObject_FillStyle = m_Object.FillStyle
  154. End Property
  155. ' Set the object's FillStyle.
  156. Public Property Let vbdObject_FillStyle(ByVal new_value As FillStyleConstants)
  157.     m_Object.FillStyle = new_value
  158. End Property
  159.  
  160. ' Return this object's bounds.
  161. Public Sub vbdObject_Bound(ByRef xmin As Single, ByRef ymin As Single, ByRef xmax As Single, ByRef ymax As Single)
  162.     m_Object.Bound xmin, ymin, xmax, ymax
  163. End Sub
  164. ' Draw the object on the canvas.
  165. Public Sub vbdObject_Draw(ByVal pic As Object)
  166.     m_Object.Draw pic
  167. End Sub
  168.  
  169. ' Set the object's Selected status.
  170. Private Property Let vbdObject_Selected(ByVal RHS As Boolean)
  171.     m_Object.Selected = RHS
  172. End Property
  173.  
  174. ' Return the object's Selected status.
  175. Private Property Get vbdObject_Selected() As Boolean
  176.     vbdObject_Selected = m_Object.Selected
  177. End Property
  178.  
  179. ' Return True if the object is at this location.
  180. Private Function vbdObject_IsAt(ByVal X As Single, ByVal Y As Single) As Boolean
  181.     vbdObject_IsAt = m_Object.IsAt(X, Y)
  182. End Function
  183.  
  184.  
  185. ' Initialize the object using a serialization string.
  186. ' The serialization does not include the
  187. ' ObjectType(...) part.
  188. Private Property Let vbdObject_Serialization(ByVal RHS As String)
  189. Dim token_name As String
  190. Dim token_value As String
  191. Dim next_x As Integer
  192. Dim next_y As Integer
  193.  
  194.     ' Start with a new polygon.
  195.     Set m_Polygon = New vbdPolygon
  196.     Set m_Object = m_Polygon
  197.  
  198.     ' Read tokens until there are no more.
  199.     Do While Len(RHS) > 0
  200.         ' Read a token.
  201.         GetNamedToken RHS, token_name, token_value
  202.         Select Case token_name
  203.             Case "IsBox"
  204.                 IsBox = CBool(token_value)
  205.             Case "vbdPolygon"
  206.                 m_Object.Serialization = token_value
  207.         End Select
  208.     Loop
  209. End Property
  210. ' Return a serialization string for the object.
  211. Public Property Get vbdObject_Serialization() As String
  212. Dim txt As String
  213.  
  214.     txt = txt & "  IsBox(" & Format$(IsBox) & ") "
  215.     txt = txt & m_Object.Serialization
  216.  
  217.     vbdObject_Serialization = "vbdScribble(" & txt & ")"
  218. End Property
  219.  
  220.